package com.mftour.common.customLabel;

import java.util.List;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

import com.mftour.common.cache.InitCache;
import com.mftour.moudles.entity.ConsolePermission;

public class PermissionTableTag extends TagSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1033225338010907426L;

	private List<ConsolePermission> allPermissionsList;

	public List<ConsolePermission> getAllPermissionsList() {
		return allPermissionsList;
	}

	public void setAllPermissionsList(List<ConsolePermission> allPermissionsList) {
		this.allPermissionsList = allPermissionsList;
	}

	private String createTr(List<ConsolePermission> cps, int level) {
		StringBuilder sb = new StringBuilder();
		for (ConsolePermission cp : cps) {
			sb.append("<tr id=\"permission" + cp.getPermissionId() + "\" onclick=\"showDetail('"+cp.getPermissionId()+"')\" title=\"单击查看详情\">");
			sb.append("<td>");
			for (int i = 0; i < level; i++) {
				sb.append("&nbsp;&nbsp;&nbsp;&nbsp;");
			}
			sb.append(cp.getPermissionName());
			sb.append("</td>");
			sb.append("<td>");
			if (cp.getParentId().equals(0)) {
				sb.append("系统");
			} else {
				sb.append(InitCache.allCategoryMap.get(cp.getParentId()).getPermissionName());
			}
			sb.append("</td>");
			sb.append("<td>");
			switch (cp.getPermissionType()) {
			case "0":
				sb.append("<span class=\"label label-success label-normal\">目录</span>");
				break;
			case "1":
				sb.append("<span class=\"label label-warning label-normal\">页面</span>");
				break;
			case "2":
				sb.append("<span class=\"label label-primary label-normal\">按钮</span>");
				break;
			case "3":
				sb.append("<span class=\"label label-danger label-normal\">其他</span>");
				break;
			}
			// sb.append(cp.getPermissionType());
			sb.append("</td>");
			sb.append("<td>");
			sb.append(cp.getSort());
			sb.append("</td>");
			sb.append("<td>");
			sb.append(cp.getPermissionIcon());
			sb.append("</td>");
			sb.append("<td><div class=\"col-sm-3\">");
			if (cp.getPermissionType().equals("0")) {
				sb.append("<button class=\"btn btn-primary btn-flat btn-shorter\" onclick=\"toAdd('" + cp.getPermissionId() + "')\">新增</button>");
			}
			sb.append("</div><div class=\"col-sm-3\"><button class=\"btn btn-warning btn-flat btn-shorter\" onclick=\"toModify('" + cp.getPermissionId() + "')\">修改</button></div>");
			if (!cp.getPermissionType().equals("0") || cp.getPermissions().size() == 0) {
				sb.append("<div class=\"col-sm-3\"> <button class=\"btn btn-danger btn-flat btn-shorter\" onclick=\"delConfirm('" + cp.getPermissionId() + "')\">删除</button></div>");
			}
			sb.append("</td>");
			sb.append("</tr>");
			if (cp.getPermissions() != null && cp.getPermissions().size() > 0) {
				sb.append(createTr(cp.getPermissions(), level + 1));
			}
		}
		return sb.toString();
	}

	@Override
	public int doEndTag() throws JspException {
		if (allPermissionsList == null) {
			allPermissionsList = InitCache.allPermissionsList;
		}

		StringBuilder sb = new StringBuilder();
		sb.append(" <table class=\"table table-hover  table-bordered\">");
		sb.append("<tbody><tr><th width=\"30%\">权限名称</th> <th width=\"30%\">所属目录</th> <th width=\"5%\">权限类型</th>  <th width=\"5%\">排序</th>  <th width=\"5%\">图标</th> <th width=\"25%\">操作</th></tr>");
		sb.append(createTr(allPermissionsList, 0));

		sb.append("</tbody></table>");
		try {
			pageContext.getOut().write(sb.toString());
			//System.out.println(sb.toString());
		} catch (Exception e) {
			e.printStackTrace();
		}
		allPermissionsList = null;

		return super.doEndTag();
	}

}
